var fishMoveData = require('./fishMoveData')();
var AllwaveData = require('./wave')();
var encrypt = require('../encryptjs/encryptjs');

function GameData() {
    this.isFaceBook = false;

    this.playTime = 0;


    this.isPause = false;
    this.gameHome = null;

    this.gameUpgrade = null;
    /*升级界面的 this*/
    this.upgradehomeScene = null;
    /*游戏界面的this*/
    this.GameLayer = null;

    /*音乐关闭*/
    this.bgMusic = true;
    this.bgEffect = true;

    /*是否展示有新鱼的界面*/
    this.gamhomeshow = false;

    /*回收  弹出得到多少金币 的回收池*/
    this.addcoinAct = new cc.NodePool();
    this.fish_Pool = new cc.NodePool();
    /*弃用了*/
    this.Allfish_pool = new cc.NodePool();
    /*回收所有鱼*/

    /*枪的攻击力 用于碰撞时扣血计算*/
    this.attack = null;
    /*这个是所有文字的集合*/
    this.gameText = {
        home: {
            text1: "new fish!",
            btn_text: "ok",
            text2: "Update aquarium to accommodate more fish"
        },
        shui: {
            text1: "aquarium",
            tishi: "Tip: Upgrade aquarium to accommodate more fish",

            btn_text: "free"
        },
        luck: {
            text1: "clover",
            tishi: "Tip：Upgrade clover to increase the chance of encountering rare fish"
        },
        watch: {
            btn_text: "free"
        },
        up_zidan: {
            dsc1: "normal spear",
            dsc2: "can hit multiple times",
            shuxing: [
                "attack",
                "rate of fire",
                "speed",
                "rope length",
                "ammo",
            ]
        }
    };

    /*绳子回收的位置坐标*/
    this.end_p = cc.v2(-280, -144);

    /*移动鱼的速度数据*/
    this.mvoeSpeedData = [1, 2, 4, 5, 6, 2, 3];
    this.version = "v1.0.2";
    this.fishData = null;
    this.getlocalData = function (_key) {
        let fishData = {
            version: "v1.0.2",
            upgrade: {
                "up_zidan": [1, 1, 1, 1, 1],
                /*  attack: 1, /*伤害*/
                //  g_speed: 1, /*攻速*/
                //  f_speed: 1, /*发射速度*/
                //  rope_len: 1, /*绳子长度*/
                //  bullet_num: 1, /*子弹数量*/*/
                "up_lucky": 1, /*当前幸运值*/
                "box": 1
            },
            game_level: 1,
            isfirstHome:false,
            isfirstGame:false,
            over: {
                /*结束界面完成度*/
                scene1: 0,
                scene2: 0,
                scene3: 0,
                scene4: 0,
            },
            handbook: {
                // newfish: [0,1,2,3,4,5,6,7,8,9,10,11,12,13],
                newfish: [],
                oldfish: [],
                // oldfish: [0,1,2,3,4,5,6,7,8,9,10,11],
            },
            coinArr: [],
            daoju: {
                zhandan: 0,
                ershi: 0,
            },
            saveTime: "",
            coinNum: 150, /*玩家在此游戏中的所有金币数*/
            fishNum: 0, /*玩家在此游戏中 所的鱼的数量*/
            unlockFish: 0,
        };
        // var _value1 = cc.sys.localStorage.getItem(_key);
        var _value1 = cc.sys.localStorage.getItem(_key);
        var _value;
        if (!_value1) {
            _value = fishData;
        } else {
            // _value = JSON.parse(_value1);
            var secretkey = 'open_sesame'; // 加密密钥
            _value = JSON.parse(encrypt.decrypt(_value1, secretkey, 256));
            if (!_value.version || this.version != _value.version) {
                _value = fishData;
            }
        }
        return _value;
    };
    /*
    *description: 通过键值对 保存数据
    *author: <zhouzhiwei>
    *@param:
    *date:
    */
    this.savelocalData = function (_value) {

        var date = new Date();    //结束时间
        _value.saveTime = date.getTime();
        var _value1 = JSON.stringify(_value);
        var secretkey = 'open_sesame'; // 加密密钥
        var encrypted = encrypt.encrypt(_value1, secretkey, 256);
        cc.sys.localStorage.setItem("fishData", encrypted);
        var str = "name=1&password=2";
        var ServerLink = "http://127.0.0.1:8081/";
        var xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status <= 400)) {
                // console.log("连接成功");
            } else {
                // console.log("连接失败")
            }
        };
        xhr.open("POST", ServerLink);
        // xhr.send(str);

    };

    this.setDate = function () {
        var myDate = new Date();
        var _y = myDate.getFullYear();    //获取完整的年份(4位,1970-????)
        var _my = myDate.getMonth();       //获取当前月份(0-11,0代表1月)
        var _r = myDate.getDate();        //获取当前日(1-31)
        var _h = myDate.getHours();
        var _m = myDate.getMinutes();
        var _s = myDate.getSeconds();
        var obj = {
            _y: _y,
            _my: _my,
            _r: _r,
            _h: _h,
            _m: _m,
            _s: _s,
        };
        return obj;

    }
    /*
    * desc: 这里统一分配数据的键
    * author: <zhouzhiwei>
    * @param:fishmess：表示的是鱼身上的绑定的信息 （血量，价值，等信息）
    * date: 2018/11/20
    */
    this.hashData = ["fishmess", "attack"];
    /*
    * desc:   数据交换中心 这里统一通过键值对 来交换数据
    * author: <zhouzhiwei>
    * @param:
    * date: 2018/11/20
    */
    this.getData = function (type, id) {
        let _data;
        switch (type) {
            case "fishmess":
                _data = this.fishMess[id];
                break;
            case "attack":
                _data = this.upgradeData.zidan[id].attack;
                break;
        }
        return _data;
    };
    /*鱼的信息*/
    this.fishMess = [
        {
            /*鱼1 */
            id: 0,
            ph: 1,
            maxph: 1,
            price: 6,
            speed: 1
        },
        {
            /*鱼2 */
            id: 1,
            ph: 2,
            maxph: 2,
            price: 9,
            speed: 1.2
        },
        {
            /*鱼3 */
            id: 2,
            ph: 4,
            maxph: 4,
            price: 12,
            speed: 1.5
        },
        {
            /*鱼4 */
            id: 3,
            ph: 8,
            maxph: 8,
            price: 15,
            speed: 2
        },
        {
            /*鱼5 */
            id: 4,
            ph: 32,
            maxph: 32,
            price: 18,
            speed: 2.5
        },
        {
            /*鱼6 */
            id: 5,
            ph: 64,
            maxph: 64,
            price: 24,
            speed: 3
        },
        {
            /*鱼6*/
            id: 6,
            ph: 128,
            maxph: 128,
            price: 32,
            speed: 4
        },
        /*上面是普通鱼*/

        {
            /*捣乱鱼1*/
            id: 7,
            ph: 1,
            maxph: 1,
            price: 10,
            speed: 1.5
        },
        {
            /*捣乱鱼2*/
            id: 8,
            ph: 8,
            maxph: 8,
            price: 20,
            speed: 2.5
        },

        {
            /*boss鱼*/
            id: 9,
            ph: 512,
            maxph: 512,
            price: 300,
            speed: 4
        },
        {
            /*补给鱼1 */
            id: 10,
            ph: 1,
            maxph: 1,
            price: 6,
            speed: 1
        },
        {
            /*补给鱼2 */
            id: 11,
            ph: 8,
            maxph: 8,
            price: 20,
            speed: 1
        },
        {
            /*反伤鱼1 */
            id: 12,
            ph: 1,
            maxph: 1,
            price: 6,
            speed: 1
        },
        {
            /*反伤鱼2 */
            id: 13,
            ph: 2,
            maxph: 2,
            price: 26,
            speed: 1
        },
        {
            /*反伤鱼3 */
            id: 14,
            ph: 8,
            maxph: 8,
            price: 60,
            speed: 1
        },

        {
            /*奖励鱼*/
            id: 15,
            ph: 8,
            maxph: 8,
            price: 200,
            speed: 4
        },
        {
            /*传染鱼*/
            id: 16,
            // ph: 2,
            ph: 128,
            maxph: 128,
            price: 30,
            speed: 4
        },


    ];
    /*升级的数据*/
    this.upgradeData = {
        box: [
            {"level": 1, "price": 0, "Allnum": 5},
            {"level": 2, "price": 500, "Allnum":8 },
            {"level": 3, "price": 700, "Allnum": 11},
            {"level": 4, "price": 1000, "Allnum": 14},
            {"level": 5, "price": 1500, "Allnum": 17},
            {"level": 6, "price": 3000, "Allnum": 19},
            {"level": 7, "price": 3500, "Allnum": 20},
            {"level": 8, "price": 5000, "Allnum": 21},
            {"level": 9, "price": 7500, "Allnum": 22},
            {"level": 10, "price": 9000, "Allnum": 23},
        ],
        luck: [
            {"level": 1, "price": 0, "gailv": 11},
            {"level": 2, "price": 300, "gailv": 13},
            {"level": 3, "price": 500, "gailv": 15},
            {"level": 4, "price": 700, "gailv": 19},
            {"level": 5, "price": 1500, "gailv": 22},
            {"level": 6, "price": 2300, "gailv": 27},
            {"level": 7, "price": 3500, "gailv": 32},
            {"level": 8, "price": 5000, "gailv": 39},
            {"level": 9, "price": 7500, "gailv": 47},
            {"level": 10, "price": 9000, "gailv": 56},

        ],
        zidan: [
            {"level": 1, "price": 0, "attack": 1, "g_speed": 1, "f_speed": 1, "rope_len": 1, "bullet_num": 16},
            {"level": 2, "price": 150, "attack": 2, "g_speed": 1.2, "f_speed": 1.2, "rope_len": 1.05, "bullet_num": 18},
            {"level": 3, "price": 500, "attack": 4, "g_speed": 1.4, "f_speed": 1.3, "rope_len": 1.1, "bullet_num": 20},
            {"level": 4, "price": 1000, "attack": 6, "g_speed": 1.6, "f_speed": 1.4, "rope_len": 1.15, "bullet_num": 22},
            {"level": 5, "price": 2500, "attack": 8, "g_speed": 1.8, "f_speed": 1.5, "rope_len": 1.2, "bullet_num": 24},
            {"level": 6, "price": 4000, "attack": 16, "g_speed": 2, "f_speed": 1.6, "rope_len": 1.25, "bullet_num": 26},
            {
                "level": 7,
                "price": 5000,
                "attack": 32,
                "g_speed": 2.2,
                "f_speed": 1.7,
                "rope_len": 1.3,
                "bullet_num": 28
            },
            {
                "level": 8,
                "price": 6000,
                "attack": 64,
                "g_speed": 2.3,
                "f_speed": 1.8,
                "rope_len": 1.35,
                "bullet_num": 30
            },
            {
                "level": 9,
                "price": 7000,
                "attack": 128,
                "g_speed": 2.4,
                "f_speed": 1.9,
                "rope_len": 1.4,
                "bullet_num": 32
            },
            {
                "level": 10,
                "price": 9000,
                "attack": 256,
                "g_speed": 2.5,
                "f_speed": 2,
                "rope_len": 1.45,
                "bullet_num": 34
            },
        ],
        lixian: [
            {"num": 1, "money": 100},
            {"num": 2, "money": 150},
            {"num": 3, "money": 200},
            {"num": 4, "money": 250},
            {"num": 5, "money": 300},
            {"num": 6, "money": 350},
            {"num": 7, "money": 400},
            {"num": 8, "money": 500},
            {"num": 9, "money": 600},
            {"num": 10, "money": 700},
            {"num": 11, "money": 800},
            {"num": 12, "money": 900},
            {"num": 13, "money": 1000},
            {"num": 14, "money": 1500},
            {"num": 15, "money": 2000},
            {"num": 16, "money": 3000},
            {"num": 17, "money": 5000},

        ]
    };

    this.puTongyuId = [0, 1, 2, 3, 4, 5, 6];
    this.putongfish = AllwaveData.putongfish;
    /*普通鱼每波出现哪些*/
    this.teshufish = AllwaveData.teshufish;
    /*特殊鱼每波出现那些*/
    this.moveData = fishMoveData.movedata;

    /*结束界面的数据*/
    this.overData = [];
    this.enter = function () {
        var bool = false;
        if (this.gameHome) {
            bool = true;
            // console.log("gameHome")
        }
        if (this.GameLayer) {
            bool = true;
            // console.log("GameLayer")
        }
        if (this.upgradehomeScene) {
            bool = true;
            // console.log("upgradehomeScene")
        }
        return true;
    }
    this.playMusic = function (str,bool) {
        if (this.bgMusic) {
            cc.audioEngine.playMusic(str, bool);
        }
    };
    this.stopMusic  = function () {
        cc.audioEngine.pauseMusic();
    };
    this.playEffect = function (str) {
        if (this.bgEffect) {
            cc.audioEngine.playEffect(str, false, 1);
        }
    };
    this.stopAllEffects = function (str) {
        cc.audioEngine.stopAllEffects();
    };
    this.pauseMusic = function (str) {
        cc.audioEngine.pauseMusic();
    };
    this.resumeMusic = function (str) {
        cc.audioEngine.resumeMusic();
    };


    /*暂停播放所有音效*/
    this.pauseAllEffects=function () {

    }
    /*回复播放所有音效*/
    this.resumeAll =function () {

    }
    /*停止播放所有音效*/
    this.stopAllEffects =function () {

    }
}

var _instance = null;
module.exports = function () {
    if (!_instance) {
        _instance = new GameData();
    }
    return _instance;
};